package com.yuneec.fly.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.yuneec.fly.R;

/**
 * Created by peng.gao on 2016/9/14.
 */
public class SeekBarPlus extends FrameLayout {
    public SeekBarPlus(Context context) {
        this(context, null);
    }

    public SeekBarPlus(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SeekBarPlus(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SeekBarPlus);
        mTitleTextView.setText(typedArray.getString(R.styleable.SeekBarPlus_my_title));
        findViewById(R.id.seekBarPlus_title_container).setVisibility(typedArray.getBoolean(R.styleable.SeekBarPlus_title_visibility, true) ? VISIBLE : GONE);
        typedArray.recycle();
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    private TextView mTitleTextView;
    private TextView mValueTextView;
    private SeekBar mSeekBar;
    private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener = null;
    private LinearLayout mSeekBarContainer;

    public TextView getValueTextView() {
        return mValueTextView;
    }

    public SeekBar getSeekBar() {
        return mSeekBar;
    }

    public void setSeekBarContainerVisibility(boolean isShow) {
        mSeekBarContainer.setVisibility(isShow ? VISIBLE : GONE);
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.seekbarplus_layout, this, false);
        addView(view);
        mSeekBarContainer = (LinearLayout) view.findViewById(R.id.seekBarPlus_seekBar_container);
        mTitleTextView = (TextView) view.findViewById(R.id.seekBarPlus_textView_title);
        mValueTextView = (TextView) view.findViewById(R.id.seekBarPlus_textView_value);
        mSeekBar = (SeekBar) view.findViewById(R.id.seekBarPlus_seekBar);
        View plusButton = view.findViewById(R.id.seekBarPlus_button_plus);
        View minusButton = view.findViewById(R.id.seekBarPlus_button_minus);

        plusButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mSeekBar.setProgress(mSeekBar.getProgress() + 1);
            }
        });

        minusButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mSeekBar.setProgress(mSeekBar.getProgress() - 1);
            }
        });

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mOnSeekBarChangeListener != null) {
                    mOnSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                if (mOnSeekBarChangeListener != null) {
                    mOnSeekBarChangeListener.onStartTrackingTouch(seekBar);
                }
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (mOnSeekBarChangeListener != null) {
                    mOnSeekBarChangeListener.onStopTrackingTouch(seekBar);
                }
            }
        });
    }

    public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener listener) {
        mOnSeekBarChangeListener = listener;
    }
}
